package com.villaday.idf.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.villaday.idf.R;
import com.villaday.idf.base.AbsRecyclerViewAdapter;
import com.villaday.idf.base.OnAdapterCallbackListener;
import com.villaday.idf.custom.ClearEditText;
import com.villaday.idf.model.allVipMembersEntity;
import com.villaday.idf.model.vipMembersEntity;
import com.villaday.idf.util.PhoneUtil;

import java.util.List;

/**
 * 设置组员的搜索列表
 */

public class SearchMemberRVAdapter extends AbsRecyclerViewAdapter<allVipMembersEntity.BodyBean> {
    private View.OnClickListener onClickListener;
    private Context mContext;
    private TextWatcher textWatcher;

    public SearchMemberRVAdapter(Context context) {
        super(context, R.layout.recyclerview_search_member_item, R.layout.recyclerview_search_member_footview);
        this.mContext = context;
    }

    @Override
    public int getItemType(allVipMembersEntity.BodyBean d) {
        return d.getType();
    }

    @Override
    public void onBindHolder(AbsRecyclerViewAdapter.RecyclerViewHolder holder, allVipMembersEntity.BodyBean d, int position) {
        switch (d.getType()) {
            case 0: {
                holder.bindSimpleDraweeView(R.id.sdv_head, d.getHeaderUrl())
                        .setOnClickListener(R.id.sdv_head);
            }
            break;
            case 1://滑到底，加载下一页
            /*    ClearEditText clearEditText = (ClearEditText) holder.getView(R.id.diagItemEtQuery);
                if (clearEditText.getTag() instanceof TextWatcher) {
                    clearEditText.removeTextChangedListener((TextWatcher) clearEditText.getTag());
                }
                clearEditText.addTextChangedListener(textWatcher);
                clearEditText.setTag(textWatcher);*/
                ClearEditText clearEditText = (ClearEditText) holder.getView(R.id.diagItemEtQuery);
//                ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams) clearEditText.getLayoutParams();
//                lp.width = PhoneUtil.getPhoneWidth(context)-(getItemCount()-2)*40;
//                clearEditText.setLayoutParams(lp);
                if (!isNormalData(position)) {
                    //根据输入框输入值的改变来过滤搜索
                    clearEditText.addTextChangedListener(new TextWatcher() {

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {
                            //当输入框里面的值为空，更新为原来的列表，否则为过滤数据列表
                            if (memeberFilterListner != null) {
                                memeberFilterListner.memberFilter(s.toString().trim());
                            }
                        }

                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count,
                                                      int after) {

                        }

                        @Override
                        public void afterTextChanged(Editable s) {
                        }
                    });
                }
                break;
        }
    }


    /**
     * 判断是否是最后一条数据
     *
     * @param position
     * @return
     */
    private boolean isNormalData(int position) {
        return position < getItemCount() - 1;
    }

    public interface OnAddMemeberFilterListner {
        void memberFilter(String s);
    }

    public OnAddMemeberFilterListner memeberFilterListner;

    public void setOnAddMemeberFilterListner(OnAddMemeberFilterListner listener) {
        memeberFilterListner = listener;
    }
}
